<template>
  <view class="index">
    <Navbar></Navbar>
    <scroll-view
      class="main"
      scroll-y
      @scrolltolower="onScrolltolower"
      refresher-enabled="true"
      :refresher-triggered="triggered"
      @refresherrefresh="onRefresh"
    >
      <!-- 轮播图 -->
      <Carousel :banners="banners" height="280rpx" />
      <!-- 前台类目 -->
      <CateScroll :list="list"></CateScroll>
      <!-- 热门推荐 -->
      <!--  热门推荐 -->
      <view class="panel recommend">
        <view class="item" v-for="item in hots" :key="item.id">
          <view class="title">
            {{ item.title }}<text>{{ item.alt }}</text>
          </view>
          <navigator
            hover-class="none"
            :url="`/pages/recommend/index?type=${item.type}`"
            class="cards"
          >
            <image
              mode="aspectFit"
              v-for="img in item.pictures"
              :key="img"
              :src="img"
            ></image>
          </navigator>
        </view>
      </view>
      <!-- 猜你喜欢 -->
      <Guess :list="likes" />
    </scroll-view>
  </view>
</template>
<script setup>
import Navbar from "./components/Navbar.vue";
import Carousel from "@/components/Carousel/Carousel.vue";
import { ref } from "vue";
import {
  getBannersAPI,
  getCategoryAPI,
  getLikesAPI,
  getHotsAPI,
} from "@/api/home";

// #region 轮播图
const banners = ref([]);
const loadBanners = async () => {
  const { result } = await getBannersAPI();
  banners.value = result;
};

loadBanners();
// #endregion

//#region 前台类目
const list = ref([]);
const getCategory = async () => {
  const { result } = await getCategoryAPI();
  list.value = result;
};
getCategory();
//#endregion

//#region 猜你喜欢
const likes = ref([]);
const pageParams = {
  page: 1,
  pageSize: 10,
};
let pages = 1;
const loadLikes = async () => {
  const { result } = await getLikesAPI(pageParams);
  pages = result.pages;
  likes.value.push(...result.items);
};
loadLikes();
const onScrolltolower = async () => {
  // 判断是否存在下一页数据
  if (pageParams.page > pages) {
    uni.showToast({ title: "没有更多数据啦", icon: "none" });
    return;
  }
  pageParams.page++;
  loadLikes();
};
onScrolltolower();
//#endregion

//#region 热门推荐
const hots = ref([]);
const getHots = async () => {
  const { result } = await getHotsAPI();
  hots.value = result;
};
getHots();
//#endregion

//#region 下拉刷新
// 开启下拉刷新，绑定刷新事件，发请求promise.all，修改loading
const triggered = ref(false);
const onRefresh = async () => {
  triggered.value = true;
  pageParams.page = 1;
  likes.value = [];
  await Promise.all([loadBanners(), getCategory(), loadLikes(), getHots()]);

  triggered.value = false;
};
//#endregion
</script>

<style lang="scss">
//
.index {
  display: flex;
  flex-direction: column;
  height: 100vh;

  .main {
    flex: 1;
    overflow: auto;
  }
}
/* 公共面板 */
.panel {
  margin: 20rpx 20rpx 0;
  border-radius: 10rpx;
  background-color: #fff;
  .title {
    display: flex;
    align-items: center;
    padding: 24rpx 24rpx 0;
    font-size: 32rpx;
    color: #262626;
    position: relative;
    text {
      font-size: 24rpx;
      color: #7f7f7f;
      margin-left: 18rpx;
    }
  }
  .more {
    position: absolute;
    right: 24rpx;
    font-size: 26rpx;
    color: #7f7f7f;
  }
  .cards {
    display: flex;
  }
  .name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

/* 推荐专区 */
.recommend {
  display: flex;
  flex-wrap: wrap;
  .item {
    display: flex;
    flex-direction: column;
    width: 50%;
    height: 254rpx;
    border-right: 1rpx solid #eee;
    border-top: 1rpx solid #eee;
    &:nth-child(2n) {
      border-right: 0 none;
    }
    &:nth-child(-n + 2) {
      border-top: 0 none;
    }
  }
  .cards {
    flex: 1;
    padding: 15rpx 20rpx;
    justify-content: space-between;
    image {
      width: 48%;
    }
  }
}
</style>
